---
--类名: MakeController.lua
--创建日期: 2017-12-22
--创建人: 陈小虎
--功能描述:
--修改内容:
--修改日期:
--修改人:
--修改备注:
--
local MakeView = import("..views.MakeView")
local MakeModel = import("..models.MakeModel")

local fileName = string.getModeName(...)
local MakeController = class(fileName, app.mvc.ControllerBase)

function MakeController:ctor(params)
	self.super.ctor(self, params)

	self._model = MakeModel.new()

	self._model:addEventListener("onMakeCompleted", function(event)
		if not cc.isDead( self._makeView ) and self._makeView:isVisible() then
			if event.ok then
				self._makeView:makeSuccess()
			else
				self._makeView:makeFaield()
			end
		end
	end)

	self._model:addEventListener("onLifeLiveUpCompleted", function(event)
		if event.ok and not cc.isDead( self._makeView ) and self._makeView:isVisible() then
			self._makeView:lifeLevelUpSuccess()
		else

		end
	end)

	self._walletHandler = UD:onChanged("wallet", function(name, old, new)
		if name == "power" and not cc.isDead( self._makeView ) and self._makeView:isVisible() then
			self._makeView:updatePowerValue(new)
		end
	end)

	self._bagHandler = UD.bag:onChanged( function( uid,item,amountGot )
		if not cc.isDead( self._makeView ) then
			self._makeView:updateItem()
		end
	end )
end

function MakeController:removeListeners()
	UD:unChanged("wallet", self._walletHandler)
	UD.bag:unChanged(self._bagHandler)

	EM:remove(self._emHandler)
	self._emHandler = nil
end

function MakeController:init()
	UIHandler:addEventListener("onOpenMake", function(event)
		self:open()
    end)
	UIHandler:addEventListener("onCloseMake", function(event)
		self:close()
    end)

	self._emHandler = EM:on("onReconnectSuccessed", function()
		self:close()
	end)
end

function MakeController:open(params)
	if cc.isDead( self._makeView ) then
		self._model:initMakeInfos()
		self._makeView = MakeView.new(self._model)
		self._makeView:switchOption(true)
		self._makeView:setCloseCallback(handler(self, self.close))
		self._makeView:setClearCallback(handler(self, self.clear))
		self._makeView:fillMakeList(params)
		self:addChild(self._makeView)
	end
	if not cc.isDead( self._makeView ) then
		self._makeView:setVisible( true )
	end
end

function MakeController:close()
	if self._makeView then
		self:removeChild(self._makeView)
		self._makeView = nil
	end
end

function MakeController:clear()
	if self._makeView then
		self._makeView = nil
	end

	UD:clearFormulaIdsNew()
end

return MakeController
